<template>
  <div class="tag-sys-dict-user">
    <el-card>
      <tag-content-header search refresh setting @toolFun="toolFun" :settingData="columns">
        <template #title>
          <span class="title-text">用户字典列表</span>
        </template>
      </tag-content-header>
      <tag-table
        ref="table"
        @refresh="getList"
        :data="dataList"
        :loading="loading"
        :columns="columns"
        :params="params"
        :total="totalSize"
        :maxHeight="maxTableHeight"
      >
        <template #operation="{ scope }">
          <div class="operation-button-box">
            <!-- <el-tooltip effect="dark" content="切换状态" placement="top" :enterable="false">
            <el-switch
              active-color="#13ce66"
              v-model="scope.row.dictStatus"
              :active-value="0"
              :inactive-value="1"
              @change="toggleStatus(scope)"
            >
            </el-switch>
          </el-tooltip> -->
            <el-tooltip effect="dark" content="编辑" placement="top" :enterable="false">
              <icon-svg name="iconEdit" class="svg-edit" @click.native="handleEdit('edit', scope.row)"></icon-svg>
            </el-tooltip>
            <el-tooltip effect="dark" content="字典数据管理" placement="top" :enterable="false">
              <i class="el-icon-tickets" @click="handleData(scope.row)"></i>
            </el-tooltip>
            <!-- <el-tooltip effect="dark" content="删除" placement="top" :enterable="false">
            <i class="el-icon-delete" @click="handleDelete(scope.row)"></i>
          </el-tooltip> -->
          </div>
        </template>
      </tag-table>
    </el-card>
    <dict-drawer ref="dictDrawer"></dict-drawer>
  </div>
</template>

<script>
import dictDrawer from './dict-user-edit-item'
export default {
  components: {
    dictDrawer
  },
  data() {
    return {
      dataList: [],
      loading: false,
      columns: [
        {
          show: true,
          label: '字典名称',
          prop: 'dictNm',
          search: true,
          searchKey: '',
          sort: true
        },
        {
          show: true,
          label: '字典类型',
          prop: 'dictType',
          search: true,
          searchKey: '',
          sort: true
        },
        {
          show: false,
          label: '状态',
          prop: 'dictStatus',
          width: '120',
          dict: 'status',
          search: true,
          searchKey: '',
          sort: true
        },
        {
          show: 'fixed',
          label: '操作',
          prop: 'operation',
          align: 'center',
          width: 200
        }
      ],
      params: {
        ascs: [],
        descs: [],
        currentPageNum: 1,
        pageSize: 10,
        queryParams: {},
        descPriority: false
      },
      totalSize: 0
    }
  },
  computed: {
    maxTableHeight() {
      // 表格最大高度 =  可视窗口高度 - 顶栏高度50 - 内容容器padding - 页面容器padding和border - 工具栏最小高度 - 描述框高度
      var paddingHeight = 0
      if (this.$route.meta.isTab) {
        paddingHeight = 70
      } else {
        paddingHeight = 30
      }
      var height = this.$store.state.common.documentClientHeight - 50 - paddingHeight - 22 - 40
      return height
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      this.$ajax({
        url: this.$ajax.setUrl('sys/sysClientDictMgr/pageList'),
        method: 'get',
        params: this.$ajax.setParams(this.params)
      }).then(({ data }) => {
        if (data && data.code === '0') {
          if (data.data.currentPageNum === this.params.currentPageNum) {
            this.dataList = data.data.records
            this.totalSize = data.data.totalSize
          }
        } else {
          this.$message.error('获取字典列表失败！')
        }
        this.loading = false
      })
    },
    toolFun(operation) {
      switch (operation) {
        case 'search':
          // 搜索动作
          this.$refs['table'].searchToggle()
          break
        case 'add':
          // 新增动作
          this.handleEdit()
          break
        case 'refresh':
          // 刷新动作
          this.getList()
          break
      }
    },
    handleEdit(type = 'add', scope = {}) {
      this.$refs.dictDrawer.showDrawer(type, scope)
    },
    toggleStatus(scope) {
      let params = {
        dictId: scope.row.dictId,
        dictNm: scope.row.dictNm,
        dictType: scope.row.dictType,
        dictStatus: scope.row.dictStatus,
        cmntStr: scope.row.cmntStr
      }
      this.$ajax({
        url: this.$ajax.setUrl('sys/sysClientDictMgr'),
        method: 'put',
        data: this.$ajax.setData(params)
      }).then(({ data }) => {
        let mes = { type: '', message: '' }
        if (data && data.code === '0') {
          mes.type = 'success'
          mes.message = '操作成功'
        } else {
          mes.type = 'error'
          mes.message = data.data || data.msg
        }
        this.getList()
        this.$message({
          message: mes.message,
          type: mes.type,
          duration: 2000
        })
      })
    },
    handleDelete(scope) {
      if (!scope.dictId) return
      this.$confirm('确认删除【' + scope.dictNm + '】？')
        .then(() => {
          let messageBox = document.getElementsByClassName('el-message-box__wrapper')
          messageBox[0].focus() // 防止按空格键重复确定
          this.$ajax({
            url: this.$ajax.setUrl('sys/sysClientDictMgr/' + scope.dictId),
            method: 'delete',
            params: this.$ajax.setParams()
          }).then(({ data }) => {
            if (data && data.code === '0') {
              this.getList()
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 2000
              })
            } else {
              this.$message.error(data.data || data.msg)
            }
          })
        })
        .catch(() => {
          return
        })
    },
    handleData(scope) {
      this.$blank('system-dict-user-data', { params: { dictId: scope.dictId, tabTitle: scope.dictNm } })
    }
  }
}
</script>

<style lang="scss"></style>
